/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
import Persistencia.exceptions.NonexistentEntityException;


/**
 *
 * @author Jhon Jairo
 */
public class AudioJpaController implements Serializable {

    public AudioJpaController() {
        emf = Persistence.createEntityManagerFactory("PizarraWPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Audio audio) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(audio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Audio audio) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            audio = em.merge(audio);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = audio.getId();
                if (findAudio(id) == null) {
                    throw new NonexistentEntityException("The audio with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Audio audio;
            try {
                audio = em.getReference(Audio.class, id);
                audio.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The audio with id " + id + " no longer exists.", enfe);
            }
            em.remove(audio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Audio> findAudioEntities() {
        return findAudioEntities(true, -1, -1);
    }

    public List<Audio> findAudioEntities(int maxResults, int firstResult) {
        return findAudioEntities(false, maxResults, firstResult);
    }

    private List<Audio> findAudioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Audio as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Audio findAudio(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Audio.class, id);
        } finally {
            em.close();
        }
    }

    public int getAudioCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Audio as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    //I add this method to find video from id course
    public List<Audio> findAudioByIdGroup(int idGroup) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createNativeQuery("SELECT * FROM `Audio` WHERE idgroup='" + idGroup + "'", Audio.class);

            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
